<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.identifier" placeholder="商品编号" class="filter-item" />
      <el-input v-model="listQuery.name$lk" placeholder="商品名称" class="filter-item" />
      <Cascader v-model="listQueryCategoryId" ref="listQueryCategoryId" placeholder="商品分类" :options="categoryData"
        :props="props" class="filter-item" style="width: 210px;" />
      <el-select v-model="listQuery.status" placeholder="状态" clearable class="filter-item">
        <el-option v-for="item in statusJson" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" type="danger" icon="el-icon-delete" @click="handlePlDelete">
        批量删除
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row row-key="id" style="width: 100%;"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" />
      <el-table-column label="序号" type="index" align="center" width="60px" />
      <!-- <el-table-column label="序号" prop="sequence" align="center" width="60px" /> -->
      <el-table-column label="商品编号" prop="identifier" align="center" />
      <el-table-column label="商品名称" prop="name" align="center" />
      <el-table-column label="商品分类" prop="categoryName" align="center" />
      <!-- <el-table-column label="编号" prop="identifier" align="center" /> -->
      <el-table-column label="状态" align="center">
        <template slot-scope="{row}">
          <span>{{ returnValueToLable(row.status,statusJson) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="定价" prop="priceText" align="center" />
      <el-table-column label="销量" prop="saleNum" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" plain size="mini" @click="toDetail(row.gongyingId,row.id)">
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import { categoryPageVoByQo } from '@/api/product'
import { collectProduct, collect } from '@/api/user'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import Cascader from '@/components/Cascader'

export default {
  name: 'CollectProductList',
  directives: { waves },
  components: { Cascader, Pagination },
  computed: {
    ...mapGetters(['userInfo'])
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        name$lk: '',
        identifier: '',
        categoryId: '',
        status: ''
      },
      multipleSelection: [],
      categoryData: [],
      props: { value: 'id', label: 'name' },
      listQueryCategoryId: '',
      statusJson: [
        { value: 0, label: '下架' },
        { value: 1, label: '上架' }
      ],
    }
  },
  watch: {
    listQueryCategoryId(val) {
      this.listQuery.categoryId = this.$refs['listQueryCategoryId'].getIds(val)
    }
  },
  created() {
    this.getList()
    this.getCategory()
  },
  methods: {
    getCategory() {
      categoryPageVoByQo().then((res) => {
        this.$set(this, 'categoryData', res || [])
      })
    },
    getList() {
      this.listLoading = true
      collectProduct(this.listQuery).then(response => {
        console.log(response);
        this.list = response.list || []
        this.total = response.totalCount
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    // 批量操作
    handleSelectionChange(val) {
      this.multipleSelection = val.map((ele) => {
        return ele.id
      })
    },
    toDetail(gongyingId, productId) {
    	this.$router.push({
    		path: "/front-shop/productDetail",
    		query: {
    			id: gongyingId,
    			productId: productId
    		}
    	});
    },
    handlePlDelete() {
      if (!this.multipleSelection || this.multipleSelection.length < 1) {
        this.$message.error('请至少选择一条数据')
        return
      }
      const params = {
        collectType: 1,
        otherId$in: this.multipleSelection.join(',')
      }
      this.$confirm('是否继续删除？').then(() => {
        collect(params).then(() => {
          // this.$notify({
          //   title: '成功',
          //   message: '批量删除成功！',
          //   type: 'success',
          //   duration: 2000
          // })
          this.$message({
            message: '批量删除成功！',
            type: 'success'
          });
          this.getList()
        })
      })
    }
  }
}
</script>
